<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier<br>
 * Name:     truncate<br>
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
 *          truncate (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
function smarty_modifier_truncate($string, $length = 80, $etc = '...',
                                  $break_words = false)
{
    if ($length == 0)
        return '';
	$source = @mb_convert_encoding($string,SYSTEM_CODE,WEB_CODE);
	$str = '';
	
	if(mb_strlen($source) > $length){
		for($i=0;$i<$length;$i++){
			if($i==($len-1)){
				$str .= (ord($source[$i]) > 0xa0) ? '' : $source[$i];
			}else{
				$str .= (ord($source[$i]) > 0xa0) ? $source[$i].$source[++$i] : $source[$i];
			}
		}
		$str = $str = @mb_convert_encoding($str,WEB_CODE,SYSTEM_CODE) . $etc;
	}else{
		$str = @mb_convert_encoding($source,WEB_CODE,SYSTEM_CODE);
	}
	
	return $str;
}
/* vim: set expandtab: */

?>
